'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	let {name_keys, coid_keys,ps_keys,mast_keys,factor_keys,ifactor_keys} = event;
	var ret = [];
	if (name_keys != undefined) {
	  const retPromises = name_keys.map(key => {
	    return uniCloud.callFunction({
	      name: "getInfo",
	      data: {
	        name_key: key
	      }
	    }).then(res => {
	      return res.result.data;
	    });
	  });
	
	  return Promise.all(retPromises).then(results => {
	    const intersection = results.reduce((commonValues, currentResult) => {
	      const commonIds = new Set(commonValues.map(obj => obj._id));
	      const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	      return commonValues.concat(uniqueObjects);
	    });
	    return intersection;
	  });
	}


	else if (ps_keys != undefined) {
		  const retPromises = ps_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				ps_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
	  const intersection = results.reduce((commonValues, currentResult) => {
	    const commonIds = new Set(commonValues.map(obj => obj._id));
	    const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	    return commonValues.concat(uniqueObjects);
	  });
	  return intersection;
	});
	}
	else if (mast_keys != undefined) {
		  const retPromises = mast_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				mast_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
	  const intersection = results.reduce((commonValues, currentResult) => {
	    const commonIds = new Set(commonValues.map(obj => obj._id));
	    const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	    return commonValues.concat(uniqueObjects);
	  });
	  return intersection;
	});
	}
	else if (coid_keys != undefined) {
		  const retPromises = coid_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				coid_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
	  const intersection = results.reduce((commonValues, currentResult) => {
	    const commonIds = new Set(commonValues.map(obj => obj._id));
	    const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	    return commonValues.concat(uniqueObjects);
	  });
	  return intersection;
	});
	}
	else if (factor_keys != undefined) {
		  const retPromises = factor_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				factor_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
	  const intersection = results.reduce((commonValues, currentResult) => {
	    const commonIds = new Set(commonValues.map(obj => obj._id));
	    const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	    return commonValues.concat(uniqueObjects);
	  });
	  return intersection;
	});
	}
	else if (ifactor_keys != undefined) {
		  const retPromises = ifactor_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				ifactor_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
	  const intersection = results.reduce((commonValues, currentResult) => {
	    const commonIds = new Set(commonValues.map(obj => obj._id));
	    const uniqueObjects = currentResult.filter(obj => !commonIds.has(obj._id));
	    return commonValues.concat(uniqueObjects);
	  });
	  return intersection;
	});
	}

};

